using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TemplateWeb.Models.Repositories
{
	#region using
	using TemplateWeb.Helpers;
	using TemplateWeb.Models.ViewModels;
	#endregion
	public class CamNangRepository
	{
		public List<CamNang> SelectAll()
		{
			return DataContext.Instance.CamNangs.OrderByDescending(p => p.NgayDang).ToList();
		}

        public List<CamNang> SelectFirstPage(int pageSize)
        {
            return DataContext.Instance.CamNangs.OrderByDescending(p => p.NgayDang).Take(pageSize).ToList();
        }

		public CamNang SelectByID(int id)
		{
			return DataContext.Instance.CamNangs.FirstOrDefault(item => item.ID == id );
		}

		public List<CamNang> SelectPaging(int page, int pageSize)
		{
            var lstItem = DataContext.Instance.CamNangs.OrderByDescending(p => p.NgayDang).ToList();

			if (pageSize > 0)
			{
				int startIndex, itemCount;
				startIndex = (page - 1) * pageSize;
				itemCount = pageSize;
				if (startIndex + itemCount > lstItem.Count)
					itemCount = lstItem.Count - startIndex;
				lstItem = lstItem.GetRange(startIndex, itemCount);
			}
			return lstItem;
		}

		public bool Insert(CamNang obj)
		{
			try
			{
				DataContext.Instance.CamNangs.InsertOnSubmit(obj);
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public bool Delete(int id)
		{
			try
			{
				var delitem = DataContext.Instance.CamNangs.FirstOrDefault(item => item.ID == id );
				DataContext.Instance.CamNangs.DeleteOnSubmit(delitem);
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public bool Save()
		{
			try
			{
				DataContext.Instance.SubmitChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public int GetTotalItem()
		{
			return DataContext.Instance.CamNangs.Count();
		}

		public int GetTotalItemByLoaiCamNang(int idloaicamnang)
		{
			return (from p in DataContext.Instance.CamNangs where p.IDLoaiCamNang == idloaicamnang select p).Count();
		}

		public int GetTotalItemByNguoiDang(Guid idnguoidang)
		{
			return (from p in DataContext.Instance.CamNangs where p.IDNguoiDang == idnguoidang select p).Count();
		}


		public int GetTotalPage()
		{
			int RecordCount = GetTotalItem();
			int PageSize = WebConfiguration.ProductsPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}

		public int GetTotalPageByLoaiCamNang(int idloaicamnang)
		{
			int RecordCount = GetTotalItemByLoaiCamNang(idloaicamnang);
			int PageSize = WebConfiguration.ProductsPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}

		public int GetTotalPageByNguoiDang(Guid idnguoidang)
		{
			int RecordCount = GetTotalItemByNguoiDang(idnguoidang);
			int PageSize = WebConfiguration.ProductsPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}


		public List<CamNang> RetrieveByID(int id)
		{
			return (from item in DataContext.Instance.CamNangs where  item.ID == id  select item).ToList();
		}

		public List<CamNang> SelectByLoaiCamNang(int idloaicamnang, int page, int pageSize)
		{
			var lstItem = (from item in DataContext.Instance.CamNangs where item.IDLoaiCamNang == idloaicamnang orderby item.NgayDang descending select item).ToList();
			if (pageSize > 0)
			{
				int startIndex, itemCount;
				startIndex = (page - 1) * pageSize;
				itemCount = pageSize;
				if (startIndex + itemCount > lstItem.Count)
					itemCount = lstItem.Count - startIndex;
				lstItem = lstItem.GetRange(startIndex, itemCount);
			}
			return lstItem;
		}

		public List<CamNang> SelectByNguoiDang(Guid idnguoidang, int page, int pageSize)
		{
			var lstItem = (from item in DataContext.Instance.CamNangs where item.IDNguoiDang == idnguoidang orderby item.NgayDang descending select item).ToList();
			if (pageSize > 0)
			{
				int startIndex, itemCount;
				startIndex = (page - 1) * pageSize;
				itemCount = pageSize;
				if (startIndex + itemCount > lstItem.Count)
					itemCount = lstItem.Count - startIndex;
				lstItem = lstItem.GetRange(startIndex, itemCount);
			}
			return lstItem;
		}

	}
}